<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductTreeView.ascx.cs" Inherits="admin_webparts_Plug_ins_Product_ProductTreeView" %>
<%@ Import Namespace="NTTHAO.CommonWeb" %>
<%@ Import Namespace="NTTHAO.Common.BL" %>
<%@ Import Namespace="NTTHAO.Web.BL.Product" %>
<link rel="stylesheet" type="text/css" href="resources/js/yui/treeview/assets/skins/sam/treeview.css" /> 
<script type="text/javascript" src="resources/js/yui/treeview/treeview.js"></script> 
<style type="text/css"> 
.whitebg {
	background-color: white;
}
</style> 
<div id="divTreeView" style="overflow: scroll; width: 250px; border: solid 1px #666; padding-top: 1px; padding-left: 1px;"  class="ygtv-highlight">
</div>
<script type="text/javascript">
    var oTreePage = new Object();
    oTreePage.data = new Array();
    oTreePage.data[0] = {id: 0, groupID: <%=Request.QueryString["g"]%>, parentID: -1, text: '<%=LanguageUtility.GetLanguageText("PRODUCT_GROUP", Session)%>'};
<%if (this._Types != null)
  {
      for (int i = 0; i < this._Types.Rows.Count; i++)
      {
          System.Data.DataRow row = this._Types.Rows[i];
          %>
      oTreePage.data[oTreePage.data.length] = {
        id: <%=row[ProductType.ID__COLUMN_NAME] %>,
        groupID: <%=row[ProductType.GROUPID__COLUMN_NAME] %>,
        parentID: <%=row[ProductType.PARENTID__COLUMN_NAME] %>,
        text: '<%=row[ProductType.NAME__COLUMN_NAME].ToString().Replace("'", "''") %>'
      }; 
      <%
      }
  }
%>
</script>
<script type="text/javascript"> 
YAHOO.util.Event.onDOMReady(function() {
    oTreePage.onResizeTreeView = function() {
        if (oTreePage.resizing) {
            return;
        }
        oTreePage.resizing = true;
        
        var oDom = YAHOO.util.Dom;
        
        oDom.setStyle('divTreeView', 'height', '1px');
        var tree = oDom.get('divTreeView');
        var oParent = tree.parentNode;

        onResizeWindow();
                
        var padding = parseInt(oDom.getStyle(oParent, 'padding-top')) + parseInt(oDom.getStyle(oParent, 'padding-bottom'));
        var borderTree = parseInt(oDom.getStyle(tree, 'border-top-width')) + parseInt(oDom.getStyle(tree, 'border-bottom-width'));
        var paddingTree = parseInt(oDom.getStyle(tree, 'padding-top')) + parseInt(oDom.getStyle(tree, 'padding-bottom'));
        var marginTree = 0;
        if(oDom.getStyle(tree, 'margin-top') && oDom.getStyle(tree, 'margin-top') != 'auto') {
            marginTree += parseInt(oDom.getStyle(tree, 'margin-top')) ;
        }
        if(oDom.getStyle(tree, 'margin-bottom') && oDom.getStyle(tree, 'margin-bottom') != 'auto') {
            marginTree += parseInt(oDom.getStyle(tree, 'margin-bottom'));
        }
        var parentHeight = oParent.clientHeight;
        
        if (oDom.getStyle('divTreeView', 'height') != '' + (parentHeight - padding - borderTree - paddingTree - marginTree - 1) + 'px') {
            oDom.setStyle('divTreeView', 'height', '' + (parentHeight - padding - borderTree - paddingTree - marginTree - 1) + 'px');
        }
        oTreePage.resizing = false;
    }
    window.onresize = oTreePage.onResizeTreeView;
    window.onresize();
});

YAHOO.util.Event.onDOMReady(function() {
    if (oTreePage && oTreePage != 'undefined') {
	    oTreePage.treeInit = function() {
            oTreePage.tree = new YAHOO.widget.TreeView("divTreeView");
            oTreePage.tree.singleNodeHighlight = true; 

		    oTreePage.buildTree();
            oTreePage.tree.draw();
            
            oTreePage.tree.expandAll(); 
            //oTreePage.tree.fireEvent('labelClick',oTreePage.tree.getNodeByIndex(0));
    		
		    oTreePage.tree.subscribe('clickEvent',function(oArgs) { 
		        oTreePage.tree.onEventToggleHighlight(oArgs);
		        
                var hiLit = oTreePage.tree.getNodesByProperty('highlightState',1);
                if (YAHOO.lang.isNull(hiLit)) { 
                    oTreePage.tree.onEventToggleHighlight(oArgs);
                }
		        oTreePage.nodeClickEvent(oArgs.node.data);
		        
		        return false;
                //alert('Click on node: ' + oArgs.node.data.text + ", id = " + oArgs.node.data.id);
            });
	    }
	    
	    oTreePage.nodeClickEvent = function(data) {
	        // Unimplement function
	    }

        oTreePage.buildTree = function() {
            for (var i = 0; i < this.data.length; i++) {
                if(this.data[i].parentID < 0) {
	                var tmpNode = new YAHOO.widget.TextNode(this.data[i].text, this.tree.getRoot(), false);
	                tmpNode.data = this.data[i];
	                this.buildChildNode(i, tmpNode);
	            }
            }
        }
        
        oTreePage.buildChildNode = function(index, node) {
            for (var i = 0; i < this.data.length; i++) {
                if(this.data[i].parentID == this.data[index].id) {
	                var tmpNode = new YAHOO.widget.TextNode(this.data[i].text, node, false);
	                tmpNode.data = this.data[i];
	                this.buildChildNode(i, tmpNode);
	            }
            }
        }
        
        oTreePage.treeInit();
    }
});
 
</script> 
